// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package role

import (
	"audio-server/internal/model"
	"audio-server/internal/sqlutil"
	"context"
	"encoding/json"

	"audio-server/internal/svc"
	"audio-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RoleMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleMenuLogic {
	return &RoleMenuLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleMenuLogic) RoleMenu(req *types.RoleMenuReq) (resp []string, err error) {

	id := req.RoleId
	roleModel := model.NewSysRoleModel(l.svcCtx.Conn)
	role, err := roleModel.FindOne(l.ctx, int64(id))
	if err != nil {
		return nil, err
	}
	ids := sqlutil.NullStringToString(role.MenuIds)

	var roleIds []string
	if ids != "" {
		err = json.Unmarshal([]byte(ids), &roleIds)
	}
	return roleIds, nil
}
